<?php
/**
 * （logic） 基础备货国家sku非FBA库存（EU备货国家）（库存来源：海外仓和国内仓）
 */
namespace App\Logic\SsBasicCountry;

use App\Models\Basics\SysArea;
use App\Models\Basics\SysPlat;
use App\Models\Inventory\AreaCn;
use App\Models\OverSeasBa\DropshippingInventoryFnsku;
use App\Models\OverSeasBa\InventoryBoxFNSku;
use App\Models\OverSeasBa\RemovaleInventoryFNSku;
use App\Models\OverSeasBa\Warehouse;
use App\Models\SsBasic\CountrySkuStock;
use App\Models\SsBasic\CountryStockingSku;
use Illuminate\Support\Facades\DB;
class InitCountryNotFBAStock
{
    /**
     * 设定：每隔*分钟运行 （country:not-fba-stock）
     */
    public static function exe(){

        try{
            $amazonPlat = SysPlat::PLAT_AMAZON; //amazon
            $areaId = SysArea::AREA_EU; //EU站点
            $warehouse = [];

            //备货国家sku
            CountryStockingSku::query()->orderBy('id')
                ->chunk(500, function ($countryStockingSkus) use (&$warehouse,$areaId,$amazonPlat){
                    DB::beginTransaction();
                    foreach ($countryStockingSkus as $stockingSku){
                        //国内库存
                        $instant = AreaCn::getInstant($areaId,$stockingSku->psku_id);
                        $transit = AreaCn::getTransit($areaId,$stockingSku->psku_id);

                        if(!array_key_exists($stockingSku->country_id,$warehouse)){
                            $warehouse[$stockingSku->country_id] = Warehouse::getIdsByCountryId($stockingSku->country_id);
                        }
                        $warehouseIds = $warehouse[$stockingSku->country_id];

                        //海外中转库存
                        $osInventory1 = InventoryBoxFNSku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$amazonPlat);
                        //海外移库库存
                        $osInventory2 = RemovaleInventoryFNSku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$amazonPlat);
                        //海外代发库存
                        $osInventory3 = DropshippingInventoryFnsku::getInstantAndTransit($warehouseIds,$stockingSku->psku_id,$amazonPlat);

                        $stockingSku->cn_instant_num = $instant;
                        $stockingSku->cn_transit_num = $transit;
                        $stockingSku->os_instant_num = ($osInventory1->instant ?? 0) + ($osInventory2->instant ?? 0) + ($osInventory3->instant ?? 0);
                        $stockingSku->os_transit_num = ($osInventory1->transit ?? 0) + ($osInventory2->transit ?? 0) + ($osInventory3->transit ?? 0);
                        $stockingSku->save();
                        $stockingSku->over_stock = CountryStockingSku::getOverStock($stockingSku);
                        $stockingSku->over_stock_value = CountryStockingSku::getOverStockValue($stockingSku->over_stock,$stockingSku->unit_price);
                        $stockingSku->save();

                        //备货站点sku库存
                        $skuStock = CountrySkuStock::query()->firstOrNew(['area_psku' => $stockingSku->area_psku]);
                        $skuStock->cn_instant_num = $instant;
                        $skuStock->cn_transit_num = $transit;
                        $skuStock->os_instant_num = ($osInventory1->instant ?? 0) + ($osInventory2->instant ?? 0) + ($osInventory3->instant ?? 0);
                        $skuStock->os_transit_num = ($osInventory1->transit ?? 0) + ($osInventory2->transit ?? 0) + ($osInventory3->transit ?? 0);
                        $skuStock->save();
                    }
                    DB::commit();
                });
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
        }
    }
}
